<?php
/**
 * @ClassName: Appversion
 * @Description: APP版本管理
 * @author: wyaohu
 * @Date: 2024-09-14 10:32
 * @Version: V1.0.0
 */

namespace app\admin\controller;

use app\common\enum\CommonEnum;
use app\common\model;
use think\Controller;
use tpext\builder\traits\HasBuilder;

class Appversion extends Controller
{
    use HasBuilder;

    protected $dataModel;

    protected function initialize()
    {
        parent::initialize();

        $this->dataModel = new model\AppVersion();
        $this->pageTitle = 'APP版本管理';
        $this->pagesize = 10;
    }

    /**
     * 构建搜索
     *
     * @return void
     */
    protected function buildSearch()
    {
        $search = $this->search;
        $search->text('new_version', '版本号');
        $search->select('app_type', 'APP类型')->options(model\AppVersion::$app_types);
        $search->select('platform_type', '平台类型')->options(model\AppVersion::$platform_types);
    }

    /**
     * 构建搜索条件
     * @return array
     */
    protected function filterWhere()
    {
        $searchData = request()->get();

        $where = [];
        if (!empty($searchData['new_version'])) {
            $where[] = ['new_version', 'like', '%' . $searchData['new_version'] . '%'];
        }
        if (isset($searchData['app_type']) && $searchData['app_type'] != '') {
            $where[] = ['app_type', 'eq', $searchData['app_type']];
        }
        if (isset($searchData['platform_type']) && $searchData['platform_type'] != '') {
            $where[] = ['platform_type', 'eq', $searchData['platform_type']];
        }
        return $where;
    }

    /**
     * 构建表格
     *
     * @return void
     */
    protected function buildTable(&$data = [])
    {
        $table = $this->table;

        $table->show('id', 'ID');
        $table->show('new_version', '版本号');
        $table->show('version_code', '版本编码');
        $table->match('app_type', 'APP类型')->options(model\AppVersion::$app_types);
        $table->match('platform_type', '平台类型')->options(model\AppVersion::$platform_types);
        $table->match('force', '强制更新')->options(CommonEnum::trueAndFalse(true));
        $table->match('full_update', '整包更新')->options(CommonEnum::trueAndFalse(true));
        $table->show('update_text', '更新内容');
        $table->show('download_url', '下载地址');

        $table->fields('create_times', '添加/更新时间')->with(
            $table->show('create_time', '添加时间'),
            $table->show('update_time', '更新时间')
        )->getWrapper()->addStyle('width:160px');

        $table->sortable('id, new_version, version_code, create_time');

        $table->getToolbar()
            ->btnAdd()
            ->btnRefresh();

        $table->getActionbar()
            ->btnEdit()
            ->btnView();
    }

    /**
     * 构建表单
     *
     * @param boolean $isEdit
     * @param array $data
     */
    protected function buildForm($isEdit, &$data = [])
    {
        $form = $this->form;

        $form->text('new_version', '版本号')->maxlength(20)->required();
        $form->radio('app_type', 'APP类型')->options(model\AppVersion::$app_types)->default('user')->required();
        $form->radio('platform_type', '平台类型')->options(model\AppVersion::$platform_types)->default('android')->required();
        $form->textarea('update_text', '更新内容')->maxlength(500)->required();
        $form->switchBtn('force', '强制更新')->default(0)->required();
        $form->switchBtn('full_update', '整包更新')->default(0)->required();
        $form->text('download_url', '下载地址')->maxlength(200)->default(domain() . '/download/app/user.apk')->required()
            ->help('APP下载地址：' . domain() . '/download/app/user.apk');

        if ($isEdit) {
            $form->show('create_time', '添加时间');
            $form->show('update_time', '更新时间');
        }
    }

    /**
     * 保存数据
     *
     * @param integer $id
     * @return void
     */
    private function save($id = 0)
    {
        $data = request()->only([
            'new_version',
            'app_type',
            'platform_type',
            'update_text',
            'force',
            'full_update',
            'download_url'
        ], 'post');

        $result = $this->validate($data, [
            'new_version|版本号' => 'require',
            'app_type|APP类型' => 'require',
            'platform_type|平台类型' => 'require',
            'update_text|更新内容' => 'require',
            'download_url|下载地址' => 'require',
        ]);

        if (!$result) {
            $this->error($result);
        }

        $replacedStr = str_replace(".", "", $data['new_version']);
        $data['version_code'] = $replacedStr;

        if ($id) {
            // 编辑
            $data['update_time'] = date_now();
        } else {
            // 新增
            $data['create_time'] = date_now();
            $data['update_time'] = date_now();
        }

//        return $this->builder()->layer()->closeRefresh();
        return $this->doSave($data, $id);
    }

}